<template>
  <div class="header">
      <div class="header_img">
          <img src="../assets/top.jpg" alt="主页图">
      </div>
      <div class="warp_nav">
        <div class="header_nav">
            <el-row :gutter="24">
                <el-col :span="3"><div class="grid-content">
                    <router-link to="/" exact>主页</router-link>
                </div></el-col>
                <el-col :span="4"><div class="grid-content">
                    <router-link  to="/information">精选博文</router-link>
                </div></el-col>
                <el-col :span="3"><div class="grid-content">
                    <router-link  to="/Album">相册</router-link>
                </div></el-col>
                <el-col :span="4"><div class="grid-content">
                    <router-link  to="/about">关于博主</router-link>
                </div></el-col>
                <el-col :span="4">
                    <div class="grid-content">
                        <router-link to="/contribute">留言反馈</router-link>
                    </div>
                </el-col>
                <el-col :span="6"><div class="grid-content user_login" v-if="!user_token">
                    <span>欢迎回来：</span>
                    <span class="user_login_name">{{user.name}}</span>
                    <span class="user_login_exit" @click="exit_user">退出</span>
                </div></el-col>
                <el-col :span="6"><div class="grid-content" v-if="user_token">
                    <span class="log_in log_">
                        <router-link to="/login">登陆</router-link>
                    </span>
                    <span class="log_zc log_">
                        <router-link to="/register">注册</router-link>
                    </span>
                </div></el-col>
            </el-row>
        </div>
      </div>
    
  </div>
</template>

<script>
export default {
    data(){
        return{
            user_token:true,
            user:{
              username: '未登录',
              id: ''
            }
        }
    },
    methods:{
        exit_user(){
            // 清除locaStorage中的uesr信息
            localStorage.removeItem('user_blog');
            // 重置登陆状态
            this.user_token = true
        }
    },
    mounted(){
        // 自定义localStrong监听事件
      window.addEventListener('setItem', (e) => {
      //获取参数
      this.user = JSON.parse(e.newValue)
      this.user_token = false
    });
   
  },
created(){
    var key=JSON.parse(localStorage.getItem("user_blog"));
    if(key==""||key==null){
     this.$message.warning('未登录！')
    }else{
      this.user = key
      this.user_token = false
    }
  }
}
</script>

<style>
.header_img{
    height: 180px;
}
.header_img > img{
    width: 100%;
    height: 100%;
}
.warp_nav{
    background-color: #aeccf8ea;
}
.header_nav{
    width: 100%;
}
.header_nav a {
    padding: 18.5px 30px;
    cursor: pointer;
    font-weight: 500;
    color: #171d24;
    font-size: 18px;
}
.header_nav a.router-link-active {
    color: #f5f5f5;
    font-size: 20px;
    background-color: #d49071;
    border-radius: 6px;
}
.header_nav a:hover{
    background-color: #d49071;
    border-radius: 6px;
}
.el-col {
    border-radius: 4px;
  }
.grid-content {
    border-radius: 4px;
    height: 60px;
    line-height: 60px;
  }
  @media screen and (min-width: 1250px) {
      .header_nav{
          width: 80%;
          margin: 0 auto;
      }
  }
  .grid-content .log_ > a{
      font-size: 13px;
  }

  /* user */
.user_login > span{
    font-size: 13px;
}
.user_login_exit,
.user_login_name{
    color: #0c72e7;
    cursor: pointer;
}
.user_login_exit{
    margin-left: 12px;;
}
</style>